package com.tenement.service.impl;

import com.tenement.dao.*;
import com.tenement.domain.*;
import com.tenement.service.GuideService;
import com.tenement.utils.Config;
import com.tenement.utils.GuideVo;
import com.tenement.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * @ClassName : GuideServiceImpl  //类名
 * @Description : ${description}  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-07-11 15:19  //时间
 */
@Service
public class GuideServiceImpl implements GuideService {

    /**
     * 客户dao
     */
    @Resource
    TenementClientMapper clientMapper;

    /**
     * 带看dao
     */
    @Resource
    TenementGuideMapper guideMapper;

    /**
     * 带看房源dao
     */
    @Resource
    TenementGuideHouseMapper guideHouseMapper;

    /**
     * 房源dao
     */
    @Resource
    TenementHouseMapper houseMapper;

    /**
     * 人员dao
     */
    @Resource
    TenementGuideUserMapper guideUserMapper;

    /**
     * 带看图片dao
     */
    @Resource
    TenementGuideImgMapper guideImgMapper;

    /**
     * 说明dao
     */
    @Resource
    TenementStreetMapper streetMapper;

    /**
     * 图片地址
     */
    @Resource
    private Config config;

    /**
     * 日志dao
     */
    @Resource
    TenementLogMapper logMapper;

    /**
     * 根据客户id查询带看信息
     *
     * @param clientId
     * @return
     */
    @Override
    public Map<String, Object> findClientId(String clientId) {
        TenementClient client = clientMapper.selectByPrimaryKey(clientId);
        Map<String, Object> map = new HashMap<>();
        map.put("clientId", client.getClientId());//客户id
        map.put("clientName", client.getClientName()); // 客户名称
        return map;
    }

    /**
     * 查询所有约带看
     *
     * @param guideId 带看房源id
     * @return map 带看房源信息
     */
    @Override
    public List<GuideVo> findHouseAll(String guideId) {
        return guideMapper.findAll(guideId);//查询带看房源
    }

    /**
     * 新增带看
     *
     * @param guideVo
     * @param user
     * @param clientId
     * @return
     */
    @Override
    public int addGuideHouse(GuideVo guideVo, TenementUser user, String clientId, String[] imgs,String ip) {

        TenementGuide guide = new TenementGuide();//约带看
        guide.setGuideId(UUID.randomUUID().toString().replaceAll("-", "")); // 带看id
        guide.setGuideName(guideVo.getGuideName()); // 带看描述
        guide.setClientId(clientId); // 关联客户id
        guide.setRevision(1); // 有效性
        guideMapper.insertSelective(guide);

        TenementGuideHouse guideHouse = new TenementGuideHouse(); // 带看房源
        guideHouse.setGuideHouseId(UUID.randomUUID().toString().replaceAll("-", "")); // 带看房源id
        guideHouse.setGuideId(guide.getGuideId()); // 带看id
        guideHouse.setHouseId(guideVo.getHouseId()); // 房源id
        guideHouseMapper.insertSelective(guideHouse);

        TenementGuideUser guideUser = new TenementGuideUser();
        guideUser.setGuideUserId(UUID.randomUUID().toString().replaceAll("-", "")); // 带看用户id
        guideUser.setGuideId(guide.getGuideId()); // 带看id
        guideUser.setUserId(user.getUserId()); // 用户id
        guideUserMapper.insertSelective(guideUser);

        if (imgs != null && imgs.length > 0) {
            List<String> list = new ArrayList<>(Arrays.asList(imgs)); // 带看图片地址
            List<TenementGuideImg> guideImgs = new ArrayList<>(); // 带看图片对象集合
            for (String s : list) {
                TenementGuideImg guideImg = new TenementGuideImg(); // 带看图片
                guideImg.setGuideImgId(UUID.randomUUID().toString().replaceAll("-", "")); // 带看图片id
                guideImg.setImgUrl(s); // 带看图片地址
                guideImg.setGuideId(guide.getGuideId()); // 带看id
                guideImgs.add(guideImg); // 新增入带看集合
            }
            guideImgMapper.insertAll(guideImgs); // 新增带看图片
        }

        TenementStreet street = new TenementStreet(); // 跟进id
        street.setStreetId(UUID.randomUUID().toString().replaceAll("-", "")); // 跟进id
        street.setUserId(user.getUserId()); // 用户id
        street.setRevision(1); // 有效性
        street.setUserName(user.getUserName()); // 用户名称
        street.setStreetName(0); // 跟进类型 0客户 1房源
        street.setSectorId(guideMapper.selectByPrimaryKey(guideHouse.getGuideId()).getClientId()); // 客户id
        street.setSectorName("带看房源：" +
                houseMapper.selectByPrimaryKey(guideHouse.getHouseId()).getUserName() +
                " 带看描述：" + guide.getGuideName()); // 跟进内容
        streetMapper.insertSelective(street); // 跟进带看信息

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("新增带看信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logGuideMod(guideVo,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(2);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);

        return 1;
    }

    /**
     * 删除带看
     *
     * @param guideHouseId
     * @return
     */
    @Override
    public int deleteGuideHouse(String guideHouseId,String ip,String UserName) {
        GuideVo guideVo=new GuideVo();
        List<TenementGuideImg> list = guideImgMapper.findGuideImgAll(guideHouseId);
        guideVo.setGuideImgs(list);
        for (TenementGuideImg img : list) {
            String[] imgUrls = img.getImgUrl().split("/");
            String imgUrl = "";
            for (int i = 0; i < imgUrls.length; i++) {
                if ("upload".equals(imgUrls[i])) {
                    imgUrl += config.getDir() + "/"; //存储的目标地址
                } else if (i == imgUrls.length - 1) {
                    imgUrl += imgUrls[i];
                } else {
                    imgUrl += imgUrls[i] + "/";
                }
            }

            File file = new File(imgUrl);
            if (file.isFile()) {
                file.delete();
            } else {
                return 0;
            }
        }

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("新增带看信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logGuideMod(guideVo,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(2);
        log.setLogIp(ip);
        log.setCreatedBy(UserName);
        logMapper.insertSelective(log);
        guideMapper.deleteByPrimaryKey(guideHouseId);
        guideUserMapper.deleteByPrimaryKey(guideHouseId);
        guideImgMapper.deleteByPrimaryKey(guideHouseId);
        return guideHouseMapper.deleteByPrimaryKey(guideHouseId);
    }

    @Override
    public int addGuideUser(TenementGuideUser guideUser) {
        guideUser.setGuideUserId(UUID.randomUUID().toString().replaceAll("-", ""));
        return guideUserMapper.insertSelective(guideUser);
    }

    @Override
    public int deleteGuideUser(String id) {
        return guideUserMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询
     *
     * @param guideId
     * @return
     */
    @Override
    public List<TenementGuideUser> findGuideUser(String guideId) {
        return guideUserMapper.findGuideUser(guideId);
    }

    @Override
    public int addImg(TenementGuideImg guideImg) {
        guideImg.setGuideImgId(UUID.randomUUID().toString().replaceAll("-", ""));
        return guideImgMapper.insertSelective(guideImg);
    }

    /**
     * 查询带看图片
     *
     * @param id
     * @return
     */
    @Override
    public List<TenementGuideImg> findGuideImgAll(String id) {
        return guideImgMapper.findGuideImgAll(id);
    }

    @Override
    public int deleteImg(String id) {
        TenementGuideImg guideImg = guideImgMapper.selectByPrimaryKey(id);
        int row = guideImgMapper.deleteByPrimaryKey(id);
        String[] imgUrls = guideImg.getImgUrl().split("/");
        String imgUrl = "";
        for (int i = 0; i < imgUrls.length; i++) {//组合存储的真实地址
            if ("upload".equals(imgUrls[i])) {
                imgUrl += config.getDir() + "/";
            } else if (i == imgUrls.length - 1) {
                imgUrl += imgUrls[i];
            } else {
                imgUrl += imgUrls[i] + "/";
            }
        }

        if (row > 0) {
            File file = new File(imgUrl);
            if (file.isFile()) {
                file.delete();
            } else {
                return 0;
            }
        }
        return 1;
    }

    @Override
    public int addGuide(TenementGuide guide) {
        return guideMapper.updateByPrimaryKeySelective(guide);
    }

    /**
     * id查询带看信息
     *
     * @param guideId
     * @return
     */
    @Override
    public GuideVo findById(String guideId) {
        GuideVo guideVo = guideMapper.findById(guideId);
        guideVo.setGuideImgs(guideImgMapper.findGuideImgAll(guideId));
        return guideVo;
    }

    /**
     * 约带看修改
     * @param guideVo
     * @param user
     * @param clientId
     * @param imgs
     * @param ip
     * @return
     */
    @Override
    public int updateGuideHouse(GuideVo guideVo, TenementUser user, String clientId, String[] imgs, String ip) {

        TenementGuide guide = guideMapper.selectByPrimaryKey(guideVo.getGuideId());//约带看
        guide.setGuideName(guideVo.getGuideName()); // 带看描述
        guide.setClientId(clientId); // 关联客户id
        guideMapper.updateByPrimaryKeySelective(guide);

        TenementGuideHouse guideHouse = guideHouseMapper.selectByGuideKey(guideVo.getGuideId()); // 带看房源
        guideHouse.setHouseId(guideVo.getHouseId()); // 房源id
        guideHouseMapper.updateByPrimaryKeySelective(guideHouse);

        TenementGuideUser guideUser = guideUserMapper.selectByGuideKey(guideVo.getGuideId());
        guideUser.setUserId(user.getUserId()); // 用户id
        guideUserMapper.updateByPrimaryKeySelective(guideUser);

        //判断是否有图片
        if (imgs != null && imgs.length > 0) {
            guideImgMapper.deleteByPrimaryKey(guideVo.getGuideId());
            List<String> lists = new ArrayList<>(Arrays.asList(imgs)); // 带看图片地址
            List<TenementGuideImg> guideImgs = new ArrayList<>(); // 带看图片对象集合
            for (String s : lists) {
                TenementGuideImg guideImg = new TenementGuideImg(); // 带看图片
                guideImg.setGuideImgId(UUID.randomUUID().toString().replaceAll("-", "")); // 带看图片id
                guideImg.setImgUrl(s); // 带看图片地址
                guideImg.setGuideId(guide.getGuideId()); // 带看id
                guideImgs.add(guideImg); // 新增入带看集合
            }
            guideImgMapper.insertAll(guideImgs); // 新增带看图片
        }

        TenementStreet street = new TenementStreet(); // 跟进id
        street.setStreetId(UUID.randomUUID().toString().replaceAll("-", "")); // 跟进id
        street.setUserId(user.getUserId()); // 用户id
        street.setRevision(1); // 有效性
        street.setUserName(user.getUserName()); // 用户名称
        street.setStreetName(0); // 跟进类型 0客户 1房源
        street.setSectorId(guideMapper.selectByPrimaryKey(guideHouse.getGuideId()).getClientId()); // 客户id
        street.setSectorName("带看房源：" +
                houseMapper.selectByPrimaryKey(guideHouse.getHouseId()).getUserName() +
                " 带看描述：" + guide.getGuideName()); // 跟进内容
        streetMapper.insertSelective(street); // 跟进带看信息
        return 1;
    }
}
